<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(
        async function () {
          const {
            testHelpers: { createDirectLineEmulator }
          } = window;

          const { directLine, store } = createDirectLineEmulator();

          const telemetryMeasurements = [];

          WebChat.renderWebChat(
            {
              activityMiddleware:
                () =>
                () =>
                ({ activity: { text = '' } }) => {
                  return () => {
                    if (~text.indexOf('error')) {
                      throw new Error('artificial error');
                    }

                    return false;
                  };
                },
              directLine,
              onTelemetry: event => {
                const { data, dimensions, duration, error, fatal, name, type, value } = event;

                telemetryMeasurements.push({
                  data,
                  dimensions,
                  duration,
                  error,
                  fatal,
                  name,
                  type,
                  value
                });
              },
              store
            },
            document.getElementById('webchat')
          );

          await pageConditions.uiConnected();

          await directLine.emulateIncomingActivity({
            text: 'error',
            type: 'message'
          });

          await expect(telemetryMeasurements.find(({ type }) => type === 'exception')?.error.message).toBe(
            'artificial error'
          );
        },
        { ignoreErrors: true }
      );
    </script>
  </body>
</html>
